package com.frxuexi.video

import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VideoData
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.video.databinding.ActivityVideoBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/video/VideoAddActivity")
@AndroidEntryPoint
class VideoAddActivity : BaseActivity<ActivityVideoBinding,VideoViewModel>() {
    var path=""
    override fun initData() {
        vm.addVideoData.observe(this){
            if(it>0){
                ToastUtils.showLong("发布视频成功")

                finish()
            }
        }
    }

    override fun initView() {
        binding.videoGsy.backButton.visibility=View.GONE
        binding.videoChoice.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()

        }

        binding.btnAdd.setOnClickListener {
            val data = VideoData(
                null,
                path,
                binding.videoTitle.text.toString(),
                Uri.parse("/sdcard/DCIM/Camera/IMG_1212.MP4").toString()
            )
            vm.addVideo(data)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            Log.i("lr",bean?.path!!)
            path=bean?.path!!
            val uri = Uri.parse("/sdcard/DCIM/Camera/IMG_1212.MP4")
//            "file:///android_asset/IMG_1212.MP4"
            binding.videoGsy.setUp(uri.toString(),false,"")
//            binding.videoGsy.startPlayLogic()
        }
    }

}